package com.example.clxy.service.impl;

import com.example.clxy.dao.ExperimentMapper;
import com.example.clxy.entity.Experiment;
import com.example.clxy.entity.Innovation;
import com.example.clxy.entity.dto.ExperimentDto;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.ExperimentResult;
import com.example.clxy.result.business.PageResult.ExperimentPageResult;
import com.example.clxy.service.ExperimentService;
import com.example.clxy.service.FileService;
import com.example.clxy.util.FileUtil;
import com.example.clxy.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/8/4 10:15
 * @Version 1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ExperimentServiceImpl implements ExperimentService {

    @Autowired
    ExperimentMapper experimentMapper;
    @Autowired
    FileService fileServiceImpl;
    @Override
    public PageResult<ExperimentPageResult> getExperimentList(String experimentname, String experimenthour, int page, int pagesize,String status) {
        PageResult<ExperimentPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        if (status!=null&&status.equals("0")) {
            List<ExperimentPageResult> list = experimentMapper.getExperimentListSimple(experimentname, experimenthour, begin, end, pagesize);
            if (list.size() > 0) {
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageIndex(page);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("获取子实验列表信息成功");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("获取子实验列表信息失败");
            }
            return result;
        } else {
            List<ExperimentPageResult> list = experimentMapper.getExperimentList(experimentname, experimenthour, begin, end, pagesize);
            if (list.size() > 0) {
                result.setTotal(list.get(0).getCnt());
                result.setPageCount(list.get(0).getPage());
                result.setPageIndex(page);
                result.setRows(list);
                result.setResult(WebRestResult.SUCCESS);
                result.setMessage("获取子实验列表信息成功");
            } else {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("获取子实验列表信息失败");
            }
            return result;
        }
    }

    /**
     * 新增子实验
     * @param experimentDto
     * @return
     */
    @Override
    public WebRestResult addExperiment(ExperimentDto experimentDto) {
        WebRestResult result = new WebRestResult();
        Experiment experiment = new Experiment();
        experimentDto.setExperimentid(UUIDUtil.randomUUID());
        BeanUtils.copyProperties(experimentDto, experiment);
        experiment.setFlag("0");
        experiment.setCreatedTime(new Date());
        experiment.setUpdatedTime(new Date());
        if (experimentMapper.insertSelective(experiment) == 1) {
            if (experimentDto.getCourselist() != null && experimentDto.getCourselist().size() > 0) {
                experimentMapper.insertExperimentMutiCourse(experimentDto);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("新增子实验成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增子实验失败");
        }
        return result;
    }

    /**
     * 修改子实验
     * @param experimentDto
     * @return
     */
    @Override
    public WebRestResult updateExperiment(ExperimentDto experimentDto) {
        WebRestResult result = new WebRestResult();
        Experiment experiment = new Experiment();
        BeanUtils.copyProperties(experimentDto, experiment);
        experiment.setUpdatedTime(new Date());
        if (experimentMapper.updateByPrimaryKeySelective(experiment) == 1) {
            if (experimentDto.getCourselist() != null && experimentDto.getCourselist().size() > 0) {
                experimentMapper.deleteExperimentMutiCourse(experimentDto.getExperimentid());
                experimentMapper.insertExperimentMutiCourse(experimentDto);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新子实验成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新子实验失败");
        }
        return result;
    }

    /**
     * 删除子实验
     * @param experimentid
     * @return
     */
    @Override
    public WebRestResult deleteExperiment(String experimentid) {
        WebRestResult result = new WebRestResult();
        if (experimentMapper.deleteExperiment(experimentid) == 1) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除子实验成功");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除子实验失败");
        }
        return result;
    }

    @Override
    public ExperimentResult getExperimentById(String experimentid) {
        ExperimentResult result = new ExperimentResult();
        ExperimentResult experiment = experimentMapper.getExperimentById(experimentid);
        if (experiment != null) {
            BeanUtils.copyProperties(experiment, result);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("根据子实验id获取子实验信息成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("根据子实验id获取子实验信息失败！");
        }
        return result;
    }

    @Override
    public WebRestResult uploadExperimentBook(MultipartFile file) {
        if (FileUtil.validateFile(file,"1").getResult() == WebRestResult.FAILURE) {
            return FileUtil.validateFile(file,"1");
        }
        WebRestResult result = new WebRestResult();

        String filepath = "experimentbook\\";
        String absolutePath = FileUtil.getAbsolutePath(filepath);
        System.out.println(absolutePath);
        String filename= file.getOriginalFilename();
        WebRestResult upload = fileServiceImpl.upload(file, absolutePath, filename);
        if (upload.getResult() == WebRestResult.FAILURE) {
            return upload;
        }
        String newFileName = (filepath + filename).replace("\\", "/");
        result.setResult(WebRestResult.SUCCESS);
        System.out.println(FileUtil.FilePathUrl);
        result.setMessage(FileUtil.FilePathUrl + newFileName);
        return result;
    }

}
